package com.example.ice.u17.util;


import com.example.ice.u17.entries.BoutiqueBean;
import com.example.ice.u17.entries.GameBean;
import com.example.ice.u17.entries.RecommendBean;
import com.example.ice.u17.global.Constants;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;


/**
 * {@link #getData(ClassType, Callback, int...)}
 *
 * 参数说明:
 * ClassType 是枚举变量
 * Callback 是回调接口,传递时请自行添加泛型,
 * int... 请求参数,请严格按照顺序传入
 *
 * 用法如下:
 * NetUtils.getData(ClassType.BoutiqueBean, new Callback<BoutiqueBean>() {
 * @Override
 * public void onResponse(Call<BoutiqueBean> call, Response<BoutiqueBean> response) {
 *
 * }
 * @Override
 * public void onFailure(Call<BoutiqueBean> call, Throwable t) {
 *
 * }
 * });
 *
 *
 *
 */
public class NetUtils {

    public static <T> void getData(ClassType classType, Callback<T> callback, int... args) {
        switch (classType) {
            case RecommendBean:
                getRecommendBean((Callback<RecommendBean>) callback, args);
                break;
            case BoutiqueBean:
                getBoutiqueBean((Callback<BoutiqueBean>) callback);
                break;
            case GameBean:
                getGameBean((Callback<GameBean>) callback, args);
                break;
        }
    }

    private static Server mServer;
    private static GameServer mGameServer;

    static {
        Retrofit.Builder builder = new Retrofit.Builder();
        mServer = builder.baseUrl(Constants.BASE_URI)
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(Server.class);
        mGameServer = builder.baseUrl(Constants.GAME_BASE_URI)
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(GameServer.class);
    }

    private static void getGameBean(Callback<GameBean> callback, int[] args) {
        if (args.length != 1) {
            throw new RuntimeException("参数args长度不匹配");
        }
        mGameServer.getGameBean(args[0]).enqueue(callback);
    }

    private static void getBoutiqueBean(Callback<BoutiqueBean> callback) {
        mServer.getBoutiqueBean().enqueue(callback);
    }

    private static void getRecommendBean(Callback<RecommendBean> callback, int[] args) {
        if (args.length != 1) {
            throw new RuntimeException("参数args长度不匹配");
        }
        mServer.getRecommendBean(args[0]).enqueue(callback);
    }

    interface Server {
        @GET("comic/recommendList")
        Call<RecommendBean> getRecommendBean(@Query("page") int page);

        @GET("comic/boutiqueList")
        Call<BoutiqueBean> getBoutiqueBean();
    }

    interface GameServer{
        @GET("v3/appIndex")
        Call<GameBean> getGameBean(@Query("page") int page);
    }
}
